package org.example.lanhang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.lanhang.VO.workervo;
import org.example.lanhang.entity.Department;
import org.example.lanhang.entity.Worker;
import org.example.lanhang.mapper.WorkerMapper;
import org.example.lanhang.service.IDepartmentService;
import org.example.lanhang.service.IWorkerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.lanhang.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gww
 * @since 2025-01-12
 */
@Service
public class WorkerServiceImpl extends ServiceImpl<WorkerMapper, Worker> implements IWorkerService {

    @Autowired
    private IDepartmentService departmentService;
    @Override
    public PageResult<Worker> findByPage(Integer page, Integer pageSize) {
        Page<Worker> page1=this.page(new Page<>(page,pageSize));

        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    @Override
    public PageResult<Worker> search(Integer page, Integer pageSize, workervo vo) {
        QueryWrapper<Worker> wrapper=new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getName()),"name",vo.getName());
        wrapper.eq(vo.getAge() != null && vo.getAge() != 0,"age",vo.getAge());
        wrapper.eq(vo.getDid()!= null && vo.getDid() != 0,"did",vo.getDid());
        Page<Worker>workerPage = this.page(new Page<>(page, pageSize), wrapper);

        workerPage.getRecords().forEach(w ->{
            Department department = departmentService.getById(w.getDid());
            w.setDname(department.getDname());
        });


        return new PageResult<>(workerPage.getRecords(),workerPage.getTotal());


    }

    @Override
    public boolean add(Worker worker) {
        return this.save(worker);
    }
}
